package com.ruoyi.web.controller.agent;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Cash;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.service.CashHistoryService;
import com.ruoyi.web.controller.app.AppController;
import com.ruoyi.web.controller.common.CheckTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "商户后台:钱包相关接口")
@RestController
@RequestMapping("/agent/cash")
public class AgentCashController extends AppController {
    @Resource
    private CashBizService cashService;

    @Resource
    private CashHistoryService cashHistoryService;


    @ApiOperation(value = "钱包详情")
    @PostMapping("info")
    public ResponseResult<Cash> info() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Cash cash = cashService.getByUserId(loginUser.getUserId());
        return this.setSuccessResult(cash);
    }


    @ApiOperation(value = "余额明细")
    @GetMapping("listHis")
    public ResponseResult<List<CashHistory>> listHis(CashHistory cashHistory) {
        SysUser user = this.getLoginUser().getUser();

        cashHistory.setUserId(user.getUserId());

        this.startPage();
        List<CashHistory> list = cashHistoryService.findList(cashHistory);
        return this.setSuccessResult(this.getPageResult(list));
    }

    @Log(title = "商户导出账单", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出明细")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CashHistory order) {
        CheckTimeUtil.checkTime(order.getParams());
        SysUser user = this.getLoginUser().getUser();
        order.setUserId(user.getUserId());
        List<CashHistory> list = cashHistoryService.list(order);
        ExcelUtil<CashHistory> util = new ExcelUtil<CashHistory>(CashHistory.class);
        util.exportExcel(response, list, "余额明细数据");
    }


}
